package warmachineroster.grapheditor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import warmachineroster.Roster;
import warmachineroster.service.validation.IValidationChangeListener;
import warmachineroster.service.validation.RosterValidationStatus;
import warmachineroster.service.validation.WarmachineRosterValidationService;
import warmachineroster.service.validation.WarmachineRosterValidationServiceImpl;
import warmachineroster.ui.views.IRoundupPage;
import warmachineroster.ui.views.RoundupPage;
import warmachinesystem.TierLevelType;

public class WarmachineRoundupPage extends RoundupPage implements IRoundupPage, IValidationChangeListener {
	
	private Label actualPointsLabel;
	private Label validStructureValueLabel;
	private Label maxPointLabel;
	private RosterValidationStatus status;

	private boolean active = false;
	private Label lblNotier;

	
	public WarmachineRoundupPage() {
	}

	@Override
	protected void refreshView() {
		if (!active){
			return;
		}
		setActualPoints(status.getTotalPointCost());
		setValidStructure(status.isValidStructure());
		setTierLevel(status.getCurrentTierLevel());
//		setActualPoints(getRoster().getPointValue());		
//		RosterVisitor rosterVisitor = WarmachineRosterVisitorFactory.buildRosterVisitor(getRoster());
//		rosterVisitor.visit();
//		setValidStructure(rosterVisitor.isValidStructure());
		setMaxPoints(getRoster().getMaxPointValue());
	}

	private void setTierLevel(TierLevelType currentTierLevel) {
		lblNotier.setText(currentTierLevel.getName());
		
	}

	protected void setValidStructure(boolean validStructure) {
		validStructureValueLabel.setText(String.valueOf(validStructure));
	}

	protected void setActualPoints(int actualPoints){
		actualPointsLabel.setText(String.valueOf(actualPoints));
	}
	
	protected void setMaxPoints(int maxPoints){
		maxPointLabel.setText(String.valueOf(maxPoints));
	}
	
	@Override
	protected void createPageControl(Composite parent) {
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FormLayout());
		
		Label pointValueLabel = new Label(composite, SWT.NONE);
		FormData fd_pointValueLabel = new FormData();
		fd_pointValueLabel.top = new FormAttachment(0, 24);
		fd_pointValueLabel.left = new FormAttachment(0, 10);
		pointValueLabel.setLayoutData(fd_pointValueLabel);
		pointValueLabel.setText("Point Value");
		
		actualPointsLabel = new Label(composite, SWT.NONE);
		FormData fd_actualPointsLabel = new FormData();
		fd_actualPointsLabel.top = new FormAttachment(pointValueLabel, 0, SWT.TOP);
		fd_actualPointsLabel.left = new FormAttachment(pointValueLabel, 6);
		actualPointsLabel.setLayoutData(fd_actualPointsLabel);
		actualPointsLabel.setText("0");
		
		Label slashLabel = new Label(composite, SWT.NONE);
		FormData fd_slashLabel = new FormData();
		fd_slashLabel.top = new FormAttachment(pointValueLabel, 0, SWT.TOP);
		fd_slashLabel.left = new FormAttachment(actualPointsLabel, 6);
		slashLabel.setLayoutData(fd_slashLabel);
		slashLabel.setText("/");
		
		maxPointLabel = new Label(composite, SWT.NONE);
		FormData fd_lblTier = new FormData();
		fd_lblTier.bottom = new FormAttachment(pointValueLabel, 0, SWT.BOTTOM);
		fd_lblTier.left = new FormAttachment(slashLabel, 6);
		maxPointLabel.setLayoutData(fd_lblTier);
		maxPointLabel.setText("15");
		
		Label validStructureLabel = new Label(composite, SWT.NONE);
		FormData fd_validStructureLabel = new FormData();
		fd_validStructureLabel.top = new FormAttachment(pointValueLabel, 9);
		fd_validStructureLabel.left = new FormAttachment(pointValueLabel, 0, SWT.LEFT);
		validStructureLabel.setLayoutData(fd_validStructureLabel);
		validStructureLabel.setText("isValidStructure");
		
		validStructureValueLabel = new Label(composite, SWT.NONE);
		FormData fd_validStructureValueLabel = new FormData();
		fd_validStructureValueLabel.top = new FormAttachment(validStructureLabel, 0, SWT.TOP);
		fd_validStructureValueLabel.left = new FormAttachment(maxPointLabel, 10, SWT.LEFT);
		validStructureValueLabel.setLayoutData(fd_validStructureValueLabel);
		validStructureValueLabel.setText("true");
		
		Label lblTierName = new Label(composite, SWT.NONE);
		FormData fd_lblTier1 = new FormData();
		fd_lblTier1.top = new FormAttachment(validStructureLabel, 16);
		fd_lblTier1.left = new FormAttachment(pointValueLabel, 0, SWT.LEFT);
		lblTierName.setLayoutData(fd_lblTier1);
		lblTierName.setText("Tier");
		
		lblNotier = new Label(composite, SWT.NONE);
		FormData fd_lblNotier = new FormData();
		fd_lblNotier.top = new FormAttachment(lblTierName, 0, SWT.TOP);
		fd_lblNotier.left = new FormAttachment(actualPointsLabel, 0, SWT.LEFT);
		lblNotier.setLayoutData(fd_lblNotier);
		lblNotier.setText("NO_TIER");
		
		active = true;
	}

	@Override
	protected void initPage() {
		//inserire qui eventuali action contributors??
		WarmachineRosterValidationService validationService = WarmachineRosterValidationServiceImpl.INSTANCE;
		validationService.register(this, getRoster());
	}

	protected Roster getRoster(){
		return (Roster)getModel();
	}
	
	
	@Override
	public void dispose(){
		WarmachineRosterValidationService validationService = WarmachineRosterValidationServiceImpl.INSTANCE;
		validationService.unregister(this, getRoster());
		super.dispose();
	}

	@Override
	public void validatedModel(RosterValidationStatus status) {
		this.status = status;
		refreshView();
	}
}
